//
//  DMLoginViewController.m
//  BaseAPP
//
//  Created by Evisu on 16/12/14.
//  Copyright © 2016年 evisum. All rights reserved.
//

#import "DMLoginViewController.h"
#import "EMError.h"
#import "EMClient.h"
#import "MainTabBarController.h"
#import "AppDelegate.h"

@interface DMLoginViewController ()

@property (nonatomic, strong)UILabel *labId;

@property (nonatomic, strong)UILabel *labPassword;

@property (nonatomic, strong)UITextField *tfId;

@property (nonatomic, strong)UITextField *tfPassword;

@property (nonatomic, strong)UIButton *btnLogin;

@property (nonatomic, strong)UIButton *btnRegiste;

@end

@implementation DMLoginViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    
    [self initViews];
    [self layoutViews];

}

- (void)initViews {

    self.view.backgroundColor = KBACKGROUND_COLOR;
    
    self.labId = [[UILabel alloc] init];
    self.labId.font = [UIFont systemFontOfSize:16.0];
    self.labId.text = @"账号";
    
    self.tfId = [[UITextField alloc] init];
    self.tfId.font = [UIFont systemFontOfSize:16.0];
    self.tfId.placeholder = @"请输入账号";
    self.tfId.borderStyle = UITextBorderStyleRoundedRect;
    
    
    self.labPassword = [[UILabel alloc] init];
    self.labPassword.text = @"密码";
    self.labPassword.font = [UIFont systemFontOfSize:16.0];
    
    self.tfPassword = [[UITextField alloc] init];
    self.tfPassword.font = [UIFont systemFontOfSize:16.0];
    self.tfPassword.placeholder = @"请输入6~16位长度的密码";
    self.tfPassword.borderStyle = UITextBorderStyleRoundedRect;
    
    self.btnLogin = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btnLogin setTitle:@"登录" forState:UIControlStateNormal];
    [self.btnLogin setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.btnLogin.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.btnLogin.backgroundColor = HEXColor(@"2384F7");
    self.btnLogin.layer.masksToBounds = YES;
    self.btnLogin.layer.cornerRadius = 10;
    [self.btnLogin addTarget:self action:@selector(didClickedLoginBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    self.btnRegiste = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btnRegiste setTitle:@"注册" forState:UIControlStateNormal];
    [self.btnRegiste setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.btnRegiste.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.btnRegiste.backgroundColor = HEXColor(@"2384F7");
    self.btnRegiste.layer.masksToBounds = YES;
    self.btnRegiste.layer.cornerRadius = 10;
    [self.btnRegiste addTarget:self action:@selector(didClickedRegisteBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.labId];
    [self.view addSubview:self.tfId];
    [self.view addSubview:self.labPassword];
    [self.view addSubview:self.tfPassword];
    [self.view addSubview:self.btnLogin];
    [self.view addSubview:self.btnRegiste];
    
}


- (void)layoutViews {

    self.labId.sd_layout
    .topSpaceToView(self.view, 100)
    .leftSpaceToView(self.view, 20)
    .heightIs(16);
    [self.labId setSingleLineAutoResizeWithMaxWidth:100];
    
    self.tfId.sd_layout
    .centerYEqualToView(self.labId)
    .leftSpaceToView(self.labId, 10)
    .widthIs(220)
    .heightIs(30);
    
    self.labPassword.sd_layout
    .topSpaceToView(self.labId, 40)
    .leftEqualToView(self.labId)
    .heightIs(16);
    [self.labPassword setSingleLineAutoResizeWithMaxWidth:100];
    
    self.tfPassword.sd_layout
    .centerYEqualToView(self.labPassword)
    .leftEqualToView(self.tfId)
    .widthIs(220)
    .heightIs(30);
    
    self.btnLogin.sd_layout
    .topSpaceToView(self.tfPassword, 50)
    .rightSpaceToView(self.view, 50)
    .widthIs(100)
    .heightIs(40);
    
    self.btnRegiste.sd_layout
    .topSpaceToView(self.tfPassword, 50)
    .leftSpaceToView(self.view, 50)
    .widthIs(100)
    .heightIs(40);
    
}

#pragma mark 注册
- (void)didClickedRegisteBtn:(UIButton *)btn {
 
    [MBProgressHUD showMessage:@"注册中" toView:self.view];
    
    [[EMClient sharedClient] registerWithUsername:self.tfId.text password:self.tfPassword.text completion:^(NSString *aUsername, EMError *aError) {
        
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        if (!aError) {
            
            [MBProgressHUD showSuccess:@"注册成功" toView:self.view];
            
        } else {
        
            
            [MBProgressHUD showError:aError.errorDescription toView:self.view];
        }
        
        
    }];
}

#pragma mark 登录
- (void)didClickedLoginBtn:(UIButton *)btn {
    
    [MBProgressHUD showMessage:@"登录中" toView:self.view];
    
    [[EMClient sharedClient] loginWithUsername:self.tfId.text password:self.tfPassword.text completion:^(NSString *aUsername, EMError *aError) {
        [MBProgressHUD hideHUDForView:self.view];
            if (!aError) {
                
                //登陆成功
                //设置自动登录
                [[EMClient sharedClient].options setIsAutoLogin:YES];

                //把最前面的视图控制器dismiss掉
                UIViewController *parentVC = self.presentingViewController;
                if (parentVC == nil) {
                    
                    //首次登录
                    MainTabBarController *mainVC = [[MainTabBarController alloc] init];
                    
                    [UIApplication sharedApplication].keyWindow.rootViewController = mainVC;
                } else {
                
                    //退出再登录
                    UIViewController *bottomVC;
                    while (parentVC) {
                        bottomVC = parentVC;
                        parentVC = parentVC.presentingViewController;
                    }
                    [bottomVC dismissViewControllerAnimated:NO completion:^{
                        //dismiss后再切换根视图
                        MainTabBarController *mainVC = [[MainTabBarController alloc] init];
                        
                        [UIApplication sharedApplication].keyWindow.rootViewController = mainVC;
                    }];
                }
                
                
            } else {
                
                //登录失败
                [MBProgressHUD showError:aError.errorDescription toView:self.view];

            }
    }];
    
    
}

- (void)dealloc {
    
    DMLog(@"dealloc");
}

@end
